package com.choosefine.it.team.service.Impl;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.service.CountService;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.json.JsonResult;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.team.dao.TeamWorkersCollectionMapper;
import com.choosefine.it.team.model.TeamWorkersCollection;
import com.choosefine.it.team.model.vo.TeamWorkersCollectionVO;
import com.choosefine.it.team.service.TeamWorkersCollectionService;
import com.choosefine.it.worker.dao.FindWorkMapper;
import com.choosefine.it.worker.model.FindWork;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("teamWorkersCollectionService")
public class TeamWorkersCollectionServiceImpl implements TeamWorkersCollectionService {

	private JsonResult result = new JsonResult();

	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Autowired
	private TeamWorkersCollectionMapper teamWorkersCollectionMapper;

	@Autowired
	private FindWorkMapper findWorkMapper;
	@Autowired
	CountService countService;

	@Override
	public Integer deleteCollection(Map<String, Object> params) throws BusinessException {
		int count=countService.isInsert(params);//看是否被收藏0代表没有1代表有
		Integer num=0;
		if(count>0) {
			num = teamWorkersCollectionMapper.cancleCollection(params);
			Integer favoriteCount = countService.favoriteCount(params);
			Update update = new Update();
			update.set("favoriteCount", favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("publishId"))), update, FindWork.class);
		}
		return num;
	}

	//@Override
	public int insertSelective(Map map) {

		int count=countService.isInsert(map);//看是否被收藏0代表没有1代表有
		if(count==0){
			int num = teamWorkersCollectionMapper.insertSelective(map);
			Integer favoriteCount=countService.favoriteCount(map);
			Update update = new Update();
			update.set("favoriteCount", favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(map.get("publishId"))),update , FindWork.class);
			return num;
		}else {
			return 0;
		}
		/*if(num > 0){
			Update update = new Update();
			update.inc("favoriteCount", 1);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(record.getWorkerpulishid())),update , FindWork.class);	
		}*/
	}

	@Override
	public TeamWorkersCollection selectbyId(Integer id) {
		return teamWorkersCollectionMapper.selectByPrimaryKey(id);
	}

	@Override
	public Page<TeamWorkersCollectionVO> selectWorkersCollectionByUserCode(Map<String,Object> params, PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<TeamWorkersCollectionVO> list= null;
		list=teamWorkersCollectionMapper.selectWorkersCollectionByUserCode(params);
		if(list.size() > 0){
			for(TeamWorkersCollectionVO vo :list){
				vo.setStayYear(DateUtils.getworkeryear(vo.getCtime()));//计算入驻年限
			}
		}
		return list;
	}
}
